﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Unity;
using Autorent.Desktop.Main;
using Autorent.Desktop.Model.AutorentMngrServiceReference;
using Autorent.Desktop.Model.Services;
using Autorent.Desktop.Modules.Autoes.Services;
using Autorent.Desktop.Modules.Autoes.Views;
using Autorent.Desktop.Modules.Events;

namespace Autorent.Desktop.Modules.Autoes.ViewModels
{
    public class AutoesListViewModel : ViewModelBase
    {
        #region Public Properties

        public AutoesListView View { get; set; }

        public Auto SelectedAuto { get; set; }

        public ObservableCollection<Auto> Autoes
        {
            get
            {
                return _repository.GetAutoes();
            }
        }

        #endregion

        #region Constructors

        public AutoesListViewModel(AutoesListView view, IAutoesRepository repository, IUnityContainer container, IRegionManager manager, IEventAggregator eventAgregator)
        {
            this.View = view;
            this.View.DataContext = this;

            this._container = container;
            this._regionManager = manager;
            this._repository = repository;
            this._eventAggregator = eventAgregator;
            this._eventAggregator.GetEvent<CollectionChangedEvent>().Subscribe(new System.Action<string>(OnPropertyChanged));
        }

        #endregion

        #region Commands

        public ICommand CreateAuto
        {
            get { return new RelayCommand(param => createAuto()); }
        }

        public ICommand RemoveAuto
        {
            get { return new RelayCommand(param => removeAuto()); }
        }

        public ICommand UpdateAuto
        {
            get { return new RelayCommand(param => updateAuto()); }
        }

        public ICommand SearchAuto
        {
            get { return new RelayCommand(param => searchAuto()); }
        }

        #endregion

        #region Private Variables

        private IAutoesRepository _repository;
        private readonly IUnityContainer _container;
        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _eventAggregator;

        #endregion

        #region Private Methods

        private void createAuto()
        {
            if (_regionManager.Regions["DetailsRegion"].GetView("CreateAutoView") == null)
            {
                foreach (var view in _regionManager.Regions["DetailsRegion"].Views)
                {
                    _regionManager.Regions["DetailsRegion"].Remove(view);
                }
                _regionManager.Regions["DetailsRegion"].Add(_container.Resolve<CreateAutoViewModel>().View, "CreateAutoView");
            }
        }

        private void removeAuto()
        {
            if (SelectedAuto != null)
            {
                _repository.DeleteAuto(SelectedAuto);

                OnPropertyChanged("Autoes");
            }
        }

        private void updateAuto()
        {
            if (SelectedAuto != null)
            {
                if (_regionManager.Regions["DetailsRegion"].GetView("UpdateAutoView") == null)
                {
                    foreach (var view in _regionManager.Regions["DetailsRegion"].Views)
                    {
                        _regionManager.Regions["DetailsRegion"].Remove(view);
                    }
                    _regionManager.Regions["DetailsRegion"].Add(_container.Resolve<UpdateAutoViewModel>(
                        new ParameterOverrides() { { "auto", SelectedAuto } }).View, "UpdateAutoView");
                }
            }
            else { MessageBox.Show("Select auto!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); }
        }

        private void searchAuto()
        {
            object view = _regionManager.Regions["SearchRegion"].GetView("SearchAutoView");

            if (view == null)
            {
                _regionManager.Regions["SearchRegion"].Add(_container.Resolve<SearchAutoViewModel>().View, "SearchAutoView");
            }
            else { _regionManager.Regions["SearchRegion"].Remove(view); }
        }

        #endregion
    }
}
